package tech.aistar.prj.filter;

import tech.aistar.prj.model.pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = "/video/*")//需要过滤的前加上check
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //考虑是否放心的前提条件是 - 有些动作是必须要登录
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        User user = (User) req.getSession().getAttribute("user");
        if(user==null){
            //尚未登录
            resp.sendRedirect("/servlet/nav/loginView");
        }else{
            //放行
            filterChain.doFilter(req, resp);
        }
    }

    @Override
    public void destroy() {

    }
}
